package com.leo.biz.service.datakeep;

import com.csvreader.CsvReader;
import com.leo.biz.po.DailyKRecord;
import com.leo.biz.po.StockBasicInfo;
import com.leo.biz.service.pub.CommUtil;
import com.leo.biz.service.pub.DaoCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by leo on 2016/7/30.
 */
@Service
public class FileReadServiceImpl {

    @Autowired
    private DaoCenter daoCenter;


    @Autowired
    private DataCommKeeper dataCommKeeper;

    public Object readDailyKRecord()
    {

                File mydir=new File("C://new_gfzq_v6_fhzx//T0002//export");
                File[] files=mydir.listFiles();
                for(int i=0;i<files.length;i++)
                {
                //    CommUtil.log(files[i].getAbsolutePath());
                    readKrecordDay(i, files.length, files[i].getAbsolutePath());
                }
                dataCommKeeper.deleteUseLessData();

                return null;

    }

    public void  readKrecordDay(int seq,int filenum,String filePath){
        float lastEndp=-999;
        List<DailyKRecord> appendList=new LinkedList<>();
        try {
            String stockno=filePath.substring(filePath.length()-10,filePath.length()-4);

            ArrayList<String[]> csvList = new ArrayList<String[]>(); //用来保存数据
            String csvFilePath =filePath;// "c:/1.csv";
            CsvReader reader = new CsvReader(csvFilePath,',', Charset.forName("gbk"));    //一般用这编码读就可以了

//            reader.readHeaders(); // 跳过表头   如果需要表头的话，不要写这句。


            while(reader.readRecord()){ //逐行读入除表头的数据
                csvList.add(reader.getValues());
            }
            reader.close();
            //System.out.println(csvList.get(0)[0].toString().split(" ")[1].toString());
            String stockname=(csvList.get(0)[0].toString().split(" ")[1].toString());

            StockBasicInfo stockBasicInfo=new StockBasicInfo();
            stockBasicInfo.setStockno(stockno);
            stockBasicInfo.setStockname(stockname);
            int ret= daoCenter.insertResult(stockBasicInfo);



            List<DailyKRecord> list1=daoCenter.queryResult(DailyKRecord.class,"where stockno='"+stockno+"' order by cldate asc");
            int maxcldate=0;
            if(list1!=null && list1.size()>0)
            {
                maxcldate=list1.get(list1.size()-1).getCldate();
            }

            int done=0;
            int total=csvList.size()-3;
            for(int row=2;row<csvList.size()-1;row++){


                done++;
                if(Integer.valueOf(csvList.get(row)[0])<=maxcldate)
                {
    //                taskProcessor.setCurProcess((seq + Float.valueOf("1.0")*((done+Float.valueOf("0.0"))/total))  / filenum);

                    continue;
                }
                String  cell = csvList.get(row)[0]; //取得第row行第0列的数据
               // System.out.print("\r\n"+cell);
                cell = csvList.get(row)[1]; //取得第row行第0列的数据
               // System.out.print(" "+cell);
                cell = csvList.get(row)[2]; //取得第row行第0列的数据
               // System.out.print(" " + cell);
                cell = csvList.get(row)[3]; //取得第row行第0列的数据
               // System.out.print(" " + cell);
                cell = csvList.get(row)[4]; //取得第row行第0列的数据
               // System.out.print(" " + cell);
                cell = csvList.get(row)[5]; //取得第row行第0列的数据
               // System.out.print(" " + cell);
                cell = csvList.get(row)[6]; //取得第row行第0列的数据
               // System.out.print(" " + cell);
                DailyKRecord krecordEveryday=new DailyKRecord();
                krecordEveryday.setStockno(stockno);
                krecordEveryday.setCldate(Integer.valueOf(csvList.get(row)[0]));
                krecordEveryday.setBegp(Float.valueOf(csvList.get(row)[1]));
                krecordEveryday.setMaxp(Float.valueOf(csvList.get(row)[2]));
                krecordEveryday.setMinp(Float.valueOf(csvList.get(row)[3]));
                krecordEveryday.setEndp(Float.valueOf(csvList.get(row)[4]));
                krecordEveryday.setVolume(Float.valueOf(csvList.get(row)[5]));
                krecordEveryday.setMoney(Float.valueOf(csvList.get(row)[6]));
                if(lastEndp==-999 && lastEndp==0)
                {
                    lastEndp=Float.valueOf(csvList.get(row)[4]);
                }else
                {
                    float incRatio=100*(Float.valueOf(csvList.get(row)[4])-lastEndp)/lastEndp;
                    lastEndp=Float.valueOf(csvList.get(row)[4]);
                    krecordEveryday.setIncratio(incRatio);
                }

                appendList.add(krecordEveryday);




        //        taskProcessor.setCurProcess((seq + Float.valueOf("1.0") * ((done + Float.valueOf("0.0")) / total)) / filenum);
            }
            int count=daoCenter.insertMultiResult(appendList);
           Float temp=count*Float.valueOf("100.0")/total;
            CommUtil.log(" [" + seq + "] " + stockno + ": " + done + "/" + total + " " + temp.toString() + "%");
        }catch(Exception ex){
            System.out.println(ex);
        }
        ;
    }
}
